package com.swfee.qinben;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.swfee.qinben.base.AppBaseActivity;
import com.swfee.qinben.util.CurenActivitUtils;

/**
 * 修改于：2013-2-28 17:03:35 修正 ListView item 点击响应失败！
 * 
 * @author ddr
 * 
 */
public class PopMenu implements OnItemClickListener {
	public interface OnItemClickListener {
		public void onItemClick(int index);
	}

	private ArrayList<Map<String, Object>> itemList;
	private Context context;
	private PopupWindow popupWindow;
	private ListView listView;
	private LayoutInflater inflater;

	public PopMenu(Context context) {
		this.context = context;

		itemList = new ArrayList<Map<String, Object>>(5);

		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		LinearLayout view = (LinearLayout) inflater.inflate(R.layout.popmenu,
				null);
		listView = (ListView) view.findViewById(R.id.listView);
		listView.setAdapter(new PopAdapter());
		listView.setOnItemClickListener(this);

		popupWindow = new PopupWindow(view, context.getResources()
				.getDimensionPixelSize(R.dimen.popmenu_width), // 这里宽度需要自己指定，使用
																// WRAP_CONTENT
																// 会很大
				context.getResources().getDimensionPixelSize(
						R.dimen.popmenu_height));
		// 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景（很神奇的）
		popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {

		switch (position) {
		case 0:
			CurenActivitUtils.toTabIndexToIntent(0);
			break;
		case 1://
			CurenActivitUtils.toTabIndexToIntent(2);
			break;
		case 2:
			CurenActivitUtils.toTabIndexToIntent(1);
			break;
		case 3:
			CurenActivitUtils.startActivity(AppointmentListActivity.class,false);
			break;
		case 4:

			break;
		case 5:
			CurenActivitUtils.toTabIndexToIntent(3);
			break;
		case 6:

			break;
		case 7:
			CurenActivitUtils.startActivity(AppLoginActivity.class, true);
			break;

		case 8:
			CurenActivitUtils.exitDialog();
			break;
		
		default:
			break;
		}
		
		dismiss();
	}

	// // 设置菜单项点击监听器
	// public void setOnItemClickListener(OnItemClickListener listener) {
	// // this.listener = listener;
	// }

	// 批量添加菜单项
	public void addItems(List<Map> items) {
		for (Map s : items)
			itemList.add(s);
	}

	// 单个添加菜单项
	public void addItem(Map item) {
		itemList.add(item);
	}

	// 下拉式 弹出 pop菜单 parent 右下角
	public void showAsDropDown(View parent) {
		// popupWindow.showAsDropDown(parent, Gravity.LEFT,
		// // 保证尺寸是根据屏幕像素密度来的
		// context.getResources().getDimensionPixelSize(R.dimen.popmenu_yoff));

		View ll = ((View) (parent.getParent())).findViewById(R.id.header_liner);
		int l = ll.getMeasuredHeight();
		int x = (int) context.getResources().getDimension(R.dimen.popmenu_offx);
		int y = (int) context.getResources().getDimension(R.dimen.popmenu_offy);
		popupWindow.showAtLocation(parent, Gravity.RIGHT, x, -l - y);

		// 使其聚集
		popupWindow.setFocusable(true);
		// 设置允许在外点击消失
		popupWindow.setOutsideTouchable(true);
		// 刷新状态
		popupWindow.update();
	}

	// 隐藏菜单
	public void dismiss() {
		popupWindow.dismiss();
	}

	// 适配器
	private final class PopAdapter extends BaseAdapter {
		@Override
		public int getCount() {
			return itemList.size();
		}

		@Override
		public Object getItem(int position) {
			return itemList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.pomenu_item, null);
				holder = new ViewHolder();
				convertView.setTag(holder);
				holder.groupItem = (TextView) convertView
						.findViewById(R.id.textView);
				ImageView iv = (ImageView) convertView
						.findViewById(R.id.imgTitle);
				iv.setImageDrawable(context.getResources().getDrawable(
						(Integer) itemList.get(position).get("imgTitle")));
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.groupItem.setText((CharSequence) itemList.get(position).get(
					"textView"));
			return convertView;
		}

		private final class ViewHolder {
			TextView groupItem;
		}
	}
}
